import { createSlice } from '@reduxjs/toolkit';
import { getNavListApi } from '@/api/nav';

const navStore = createSlice({
  name: 'nav',
  // 初始化state
  initialState: {
    searchList: [
      {
        value: 'https://www.baidu.com/s?wd=',
        label: '百度',
      },
      {
        value: 'https://cn.bing.com/search?q=',
        label: '必应',
      },
      {
        value: 'https://www.google.com.hk/search?q=',
        label: '谷歌',
      },

      {
        value: 'https://www.sogou.com/tx?query=',
        label: '搜狗',
      },
    ],
    list: [
      /* {
        type: '框架函数库',
        list: [
          {
            nav_id: 219,
            nav_title: 'wgcloud服务器监控',
            nav_desc: '详情',
            nav_url: 'https://www.wgstart.com/',
            nav_icon:
              'https://www.wgstart.com/wgcloud/images/apple-touch-icon-120x120.png',
            type_num: 1,
            nav_sort: 0,
            create_time: '2022-05-25T01:50:24.000Z',
            classify_id: 1,
            classify_title: '框架函数库',
            classify_sort: 1,
            update_time: '2022-05-25T01:50:28.000Z',
            status: 1,
            remark: '123',
            encoding: null,
            parent: '0000000000',
          },
        ],
      }, */
    ],
    footerList: [
      { text: 'XZD', url: '/' },
      {
        text: '浙ICP备2021013242号-1',
        url: 'https://beian.miit.gov.cn/',
      },
    ],
  },
  // reducer 函数 修改数据的同步方法
  reducers: {
    setList(state, action) {
      state.list = action.payload;
    },
  },
});

const { setList } = navStore.actions;
//异步请求
const getNavList = () => {
  return async (dispatch) => {
    const res = await getNavListApi();
    dispatch(setList(res.data));
  };
};

const navReducer = navStore.reducer;
export { getNavList };
export default navReducer;
